﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Benefit.Models.View
{
    /// <summary>
    /// 交易员日资金情况
    /// </summary>
    public class OperatorDayMoney
    {
        public int Id { get; set; }
        /// <summary>
        /// 操作员编号
        /// </summary>
        public int OperatorId { get; set; }

        public virtual Sys.Operator Operator { get; set; }

        public int TradeHistoryId { get; set; }

        public virtual Sys.TradeHistory TradeHistory { get; set; }

        /// <summary>
        /// 多仓持仓
        /// </summary>
        public int LongPosition { get; set; }
        /// <summary>
        /// 空仓持仓
        /// </summary>
        public int ShortPosition { get; set; }
        /// <summary>
        /// 期初结存
        /// </summary>
        public double PresetTlePosit { get; set; }
        /// <summary>
        /// 期末结存
        /// </summary>
        public double SettlePosit { get; set; }
        /// <summary>
        /// 冻结保证金
        /// </summary>
        public double FrozenMargin { get; set; }
        /// <summary>
        /// 冻结手续费
        /// </summary>
        public double FrozenCommission { get; set; }
        /// <summary>
        /// 入金金额
        /// </summary>
        public double Deposit { get; set; }
        /// <summary>
        /// 出金金额
        /// </summary>
        public double WithDraw { get; set; }
        /// <summary>
        /// 交割保证金
        /// </summary>
        public double DeliveryMargin { get; set; }
        /// <summary>
        /// 交割手续费
        /// </summary>
        public double DeliveryCommission { get; set; }
        /// <summary>
        /// 追加保证金
        /// </summary>
        public double AppendMargin { get; set; }
        /// <summary>
        /// 基础保证金
        /// </summary>
        public double BaseMargin { get; set; }
        /// <summary>
        /// 质押金额
        /// </summary>
        public double Mortgage { get; set; }
        /// <summary>
        /// 上次质押金额
        /// </summary>
        public double Premortgage { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Commission { get; set; }
        /// <summary>
        /// 可用资金
        /// </summary>
        public double AvailableMoney { get; set; }
        /// <summary>
        /// 可取资金
        /// </summary>
        public double WithDrawQuota { get; set; }
        /// <summary>
        /// 动态权益
        /// </summary>
        public double DynamicMoney { get; set; }
        /// <summary>
        /// 占用保证金
        /// </summary>
        public double Margin { get; set; }
        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double CloseProfit { get; set; }
        /// <summary>
        /// 持仓盈亏
        /// </summary>
        public double PositionProfit { get; set; }
        /// <summary>
        /// 风险度
        /// </summary>
        public double RiskRate { get; set; }

        ///// <summary>
        ///// 得到交易日账户信息
        ///// </summary>
        ///// <param name="accountMoneys"></param>
        ///// <returns></returns>
        //public OperatorDayMoney GetOperatorDayMoney(List<Data.Day.AccountDayMoney> accountMoneys, int operatorId, int tradeHistoryId)
        //{
        //    if (accountMoneys.Count > 0)
        //    {
        //        OperatorDayMoney money = new OperatorDayMoney();
        //        money.OperatorId = operatorId;
        //        money.TradeHistoryId = tradeHistoryId;
        //        money.AppendMargin = accountMoneys.Sum(a => a.AppendMargin);
        //        money.AvailableMoney = accountMoneys.Sum(a => a.AvailableMoney);
        //        money.BaseMargin = accountMoneys.Sum(a => a.BaseMargin);
        //        money.CloseProfit = accountMoneys.Sum(a => a.CloseProfit);
        //        money.Commission = accountMoneys.Sum(a => a.Commission);
        //        money.DeliveryCommission = accountMoneys.Sum(a => a.DeliveryCommission);
        //        money.DeliveryMargin = accountMoneys.Sum(a => a.DeliveryMargin);
        //        money.Deposit = accountMoneys.Sum(a => a.Deposit);
        //        money.DynamicMoney = accountMoneys.Sum(a => a.DynamicMoney);
        //        money.FrozenCommission = accountMoneys.Sum(a => a.FrozenCommission);
        //        money.FrozenMargin = accountMoneys.Sum(a => a.FrozenMargin);
        //        money.LongPosition = accountMoneys.Sum(a => a.LongPosition);
        //        money.Margin = accountMoneys.Sum(a => a.Margin);
        //        money.Mortgage = accountMoneys.Sum(a => a.Mortgage);
        //        money.PositionProfit = accountMoneys.Sum(a => a.PositionProfit);
        //        money.Premortgage = accountMoneys.Sum(a => a.Premortgage);
        //        money.PresetTlePosit = accountMoneys.Sum(a => a.PresetTlePosit);
        //        money.RiskRate = accountMoneys.Sum(a => a.RiskRate);
        //        money.SettlePosit = accountMoneys.Sum(a => a.SettlePosit);
        //        money.ShortPosition = accountMoneys.Sum(a => a.ShortPosition);
        //        money.WithDraw = accountMoneys.Sum(a => a.WithDraw);
        //        money.WithDrawQuota = accountMoneys.Sum(a => a.WithDrawQuota);
        //        return money;
        //    }
        //    else
        //    {
        //        return null;
        //    }
        //}


        ///// <summary>
        ///// 初始化操作员资金信息
        ///// </summary>
        ///// <param name="tradeHistoryId"></param>
        ///// <param name="db"></param>
        //public void InitOperatorDayMoney(int tradeHistoryId, DB.DBManager db)
        //{
        //    var operators = db.Operators.ToList();
        //    foreach (Sys.Operator op in operators)
        //    {
        //        List<Data.Day.AccountDayMoney> moneys = new List<Data.Day.AccountDayMoney>();
        //        List<Sys.Account> accounts = new Sys.DayOperatorAccount().GetDayOperatorAccounts(new Sys.TradeHistory().GetDateTimeFromTradeHistoryId(tradeHistoryId, db), op.Id, db);
        //        foreach (Sys.Account a in accounts)
        //        {
        //            Data.Day.AccountDayMoney money = new Data.Day.AccountDayMoney().GetAccountDayMoney(tradeHistoryId, a.Id, db);
        //            moneys.Add(money);
        //        }
        //        OperatorDayMoney _money = GetOperatorDayMoney(moneys, op.Id, tradeHistoryId);
        //        if (_money != null)
        //        {
        //            db.OperatorDayMoney.Add(_money);
        //        }
        //    }
        //    db.SaveChanges();
        //}

        //public void Delete(int tradeHistoryId, DB.DBManager db)
        //{
        //    var query = db.OperatorDayMoney.Where(a => a.TradeHistoryId == tradeHistoryId);
        //    foreach (OperatorDayMoney m in query)
        //    {
        //        db.OperatorDayMoney.Remove(m);
        //    }
        //    db.SaveChanges();
        //}

        //public OperatorDayMoney GetOperatorDayMoney(int operatorid, int tradeHistoryId, DB.DBManager db)
        //{
        //    var query = db.OperatorDayMoney.Where(a => a.OperatorId == operatorid).Where(a => a.TradeHistoryId == tradeHistoryId);
        //    if (query.Count() > 0)
        //    {
        //        return query.First();
        //    }
        //    else
        //    {
        //        return new OperatorDayMoney();
        //    }
        //}
    }
}
